/*
 *  Ship.cpp
 *  SpaceTrader
 *
 *  Created by Jake on 9/24/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */
#include <iostream>
#include "Ship.h"
#include "SpaceTrader.h"

Ship::Ship(int attack, int defense, int speed, int health, const std::string image) : attack(attack),
                                    defense(defense), speed(speed), health(health), bounds(SDL_Rect()), xVel(0), yVel(0),
                                    fuelCapacity(300), currFuel(300), icon(NULL), lasers(NULL), isDestroyed(false)
{
	icon = load_image(image+".png",false);
	
	if ( icon == NULL ) {
		std::cerr << "Error: Loading default Ship 1st constructor Image" << std::endl;
	}
	
	bounds.x = 100;
	bounds.y = 100;
	bounds.w = icon->w;
	bounds.h = icon->h;
}

Ship::Ship(int attack, int defense, int speed, int health, int yVel, const std::string image) : attack(attack),
defense(defense), speed(speed), health(health), bounds(SDL_Rect()), xVel(0), yVel(yVel),
fuelCapacity(300), currFuel(300), icon(NULL), lasers(NULL), isDestroyed(false)
{
	icon = load_image(image+".png",false);
	
	if ( icon == NULL ) {
		std::cerr << "Error: Loading default Ship 1st constructor Image" << std::endl;
	}
	
	bounds.x = 100;
	bounds.y = 100;
	bounds.w = icon->w;
	bounds.h = icon->h;
}

Ship::Ship() : attack(1), defense(1), speed(1), health(10), bounds(SDL_Rect()), xVel(0), yVel(0), fuelCapacity(200), currFuel(200),
                icon(NULL), lasers(NULL), isDestroyed(false) { }

Ship::Ship(const Ship &S) : attack(S.attack), defense(S.defense), speed(S.speed),
                            health(S.health), bounds(S.bounds), xVel(S.xVel), yVel(S.yVel),
                            fuelCapacity(S.fuelCapacity), currFuel(S.currFuel),
                            icon(S.icon), lasers(std::vector<Laser*>(S.lasers.size())), isDestroyed(false)
{
    for (unsigned int i = 0; i < lasers.size(); i++){
        lasers[i] = S.lasers[i];
    }
}

Ship::~Ship(){
    delete icon;
    for (unsigned int i = 0; i < lasers.size(); i++){
        delete lasers[i];
    }
}

Ship& Ship::operator=(const Ship &S){
    if (&S != this){
        delete icon;
        for (unsigned int i = 0; i < lasers.size(); i++){
            delete lasers[i];
        }
        attack = S.attack;
        defense = S.defense;
        speed = S.speed;
        health = S.health;
        bounds = S.bounds;
        xVel = S.xVel;
        yVel = S.yVel;
        fuelCapacity = S.fuelCapacity;
        currFuel = S.currFuel;
        icon = S.icon;
        lasers = std::vector<Laser*>(S.lasers.size());
        for (unsigned int i = 0; i < lasers.size(); i++){
            lasers[i] = S.lasers[i];
        }
    }
    return *this;
}


//Equipment Ship::getEquipment() {}

//int *Ship::getStats() {}

//void Ship::setStats() {}


int Ship::getFuel() {
	return currFuel;
}

void Ship::setFuel(int fuel) {
    if (fuel >= 0){
        currFuel = fuel;
    } else {
        currFuel = 0;
    }
}


/*
 * Handles events for Player
 */
void Ship::handle_input(SDL_Event event)
{
	//If a key was pressed
	int sensitivity = 8;
	if ( event.type == SDL_KEYDOWN ) {
		//Adjust the velocity if applicable
		switch ( event.key.keysym.sym ) {
			case SDLK_UP: yVel -= bounds.h / sensitivity; break;
			case SDLK_DOWN: yVel += bounds.h / sensitivity; break;
            default:;
		}
	}
	//If a key was released
	else if ( event.type == SDL_KEYUP ) {
		//Adjust the velocity if applicable
		switch ( event.key.keysym.sym ) {
			case SDLK_UP: yVel += bounds.h / sensitivity; break;
			case SDLK_DOWN: yVel -= bounds.h / sensitivity; break;
			case SDLK_SPACE:
				lasers.push_back(new Laser(bounds.x+70, bounds.y+30, yVel, "laser"));
				lasers.push_back(new Laser(bounds.x+70, bounds.y+70, yVel, "laser"));
				break;
            default:;
		}
	}
}


/*
 *	Takes in an array of collidable and checks for collisions
 */
void Ship::move()//SDL_Rect *collidable)
{
	//Move the Player left or right, relative to the velocity
	/*
    if (bounds.x > -1*icon->w){
        bounds.x = (bounds.x+xVel)%SCREEN_WIDTH;
    } else {
        bounds.x = SCREEN_WIDTH-1;
    }
    if (bounds.y > -1*icon->h){
        bounds.y = (bounds.y+yVel)%SCREEN_HEIGHT;
    } else {
        bounds.y = SCREEN_HEIGHT-1;
    } */
	if (bounds.y + yVel > 0 && bounds.y + bounds.h + yVel < SCREEN_HEIGHT)
		bounds.y += yVel;
	else if (bounds.y + yVel <= 0)
		bounds.y = 0;
	else if (bounds.y + bounds.h + yVel >= SCREEN_HEIGHT)
		bounds.y = SCREEN_HEIGHT - bounds.h;
	//If the Player had a collision
	/*if ( (coll.x < 0 ) || ( coll.x + coll.w > SCREEN_WIDTH) ) {
		std::String coll_res = collidable.check_collision( coll );
		
		if ( std::strcmp(coll_res,"bullet") ) {
			//handle bullet hit
		} else if ( std::strcmp(coll_res,"wall") ) {
			//handle wall hit
		}
	}*/
}

bool Ship::checkIfHit(std::vector<Laser*> lasers){
    int count = 0;
    for (unsigned int i = 0; i < lasers.size(); i++){
        if (check_collision(this->bounds, lasers[i]->getBounds()) && !isDestroyed){
            lasers[i]->destroy();
            count++;
        }
    }
    if (count > 0){
        isDestroyed = true;
        return true;
    }
    return false;
}

int Ship::getCapacity(){
    return fuelCapacity;
}


/*
 *	Renders the player on the screen 
 */
void Ship::show(SDL_Surface *screen) 
{
    if (!isDestroyed) apply_surface( bounds.x, bounds.y, icon, screen);
}

std::vector<Laser*> Ship::getLasers()
{
	return lasers;
}

int Ship::getX()
{
	return bounds.x;
}

int Ship::getY()
{
	return bounds.y;
}

int Ship::getH()
{
	return bounds.h;
}

void Ship::setLasers(std::vector<Laser*> lasers)
{
	this->lasers = lasers;
}

void Ship::setX(int x)
{
	bounds.x = x;
}

void Ship::setY(int y)
{
	bounds.y = y;
}

int Ship::getXVel(){
    return xVel;
}

void Ship::setXVel(int v){
    xVel = v;
}

int Ship::getYVel(){
    return yVel;
}

void Ship::setYVel(int v){
    yVel = v;
}

void Ship::shoot(){
    if (!isDestroyed){
        lasers.push_back(new Laser(bounds.x, bounds.y+75, -10, yVel, "laser"));
        lasers.push_back(new Laser(bounds.x, bounds.y+25, -10, yVel, "laser"));
    }
}

